package ihm;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

import dao.DaoCollaborateurMySql;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Font;
import metier.Collaborateur;
import metier.Tache;
import metier.Contrat;
import java.awt.SystemColor;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class Maquette extends JFrame implements MouseListener, ActionListener {
	/**
	 * .
	 */
	private static final long serialVersionUID = 1L;


	private JTextField txtNombreJourhomme;
	private JTextField txtPrenom;
	private JTextField txtNom;
	private JTextField txtMatriculeCollaborateur;
	private JTextField txtReferenceContrat;
	private JTextField txtClient;
	private JTextField txtObjetContrat;
	private JComboBox<Integer> cbxMatricule;
	private ArrayList<Collaborateur> listCollab;
	public DaoCollaborateurMySql dao;
	private JLayeredPane lydPaneListerCollaborateurs;
	private JTabbedPane Onglets;
	private boolean declencheur = true;
	
	public void chargerListeCollab() {
		listCollab = dao.getAll();
		declencheur = false;
		cbxMatricule.removeAllItems();
		declencheur = true;
		Collaborateur unCollab;
		for (int i = 0;i < listCollab.size() ; i++){
			unCollab = listCollab.get(i);
			cbxMatricule.addItem(unCollab.getMatricule());
		}
		unCollab = dao.getOne((int) cbxMatricule.getSelectedItem());
		if(cbxMatricule.getSelectedIndex() != 0){
			cbxMatricule.setSelectedIndex(cbxMatricule.getSelectedIndex() - 1);
			unCollab = dao.getOne((int) cbxMatricule.getSelectedItem());
		}
		//Initialiser les champs Nom et Pr�nom
		txtPrenom.setText(unCollab.getPrenom());
		txtNom.setText(unCollab.getNom());
		System.out.println("Chargement liste");
		}
	
	public Maquette() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				quitterAppli();
			}
		});
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		// D�claration de Collaborateurs
				dao = new DaoCollaborateurMySql();
				cbxMatricule = new JComboBox<Integer>();
		// D�claration de t�ches
		// � remplir
		// D�claration de contrats

		getContentPane().setBackground(Color.WHITE);

		JPanel panneauPrincipal = new JPanel();
		getContentPane().add(panneauPrincipal, BorderLayout.CENTER);
		panneauPrincipal.setLayout(null);

		Onglets = new JTabbedPane(JTabbedPane.TOP);
		Onglets.setBounds(0, 0, 444, 239);
		panneauPrincipal.add(Onglets);

		JLayeredPane lydPaneListerTaches = new JLayeredPane();
		lydPaneListerTaches.setBackground(SystemColor.inactiveCaption);
		Onglets.addTab("Taches", null, lydPaneListerTaches, null);
		lydPaneListerTaches.setLayout(null);

		JLabel lblLibelleTache = new JLabel("Libelle Tache :");
		lblLibelleTache.setBounds(10, 82, 71, 14);
		lblLibelleTache.setAlignmentX(Component.RIGHT_ALIGNMENT);
		lydPaneListerTaches.add(lblLibelleTache);

		JLabel lblTache = new JLabel("TACHE");
		lblTache.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTache.setForeground(Color.BLUE);
		lblTache.setBounds(10, 11, 71, 14);
		lydPaneListerTaches.add(lblTache);

		JLabel lblNombreJourhomme = new JLabel("Nombre Jour/Homme :");
		lblNombreJourhomme.setBounds(10, 107, 107, 14);
		lydPaneListerTaches.add(lblNombreJourhomme);

		JLabel lblMatriculeCollaborateur = new JLabel(
				"Matricule collaborateur :");
		lblMatriculeCollaborateur.setBounds(10, 132, 120, 14);
		lydPaneListerTaches.add(lblMatriculeCollaborateur);

		JLabel lblReferenceContrat = new JLabel("Référence contrat :");
		lblReferenceContrat.setBounds(10, 157, 107, 14);
		lydPaneListerTaches.add(lblReferenceContrat);

		txtNombreJourhomme = new JTextField();
		txtNombreJourhomme.setEditable(false);
		txtNombreJourhomme.setBounds(140, 104, 28, 20);
		lydPaneListerTaches.add(txtNombreJourhomme);
		txtNombreJourhomme.setColumns(10);

		JButton btnAjouterUneTache = new JButton("Ajouter une tache");
		btnAjouterUneTache.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame monAjoutTache = new ajoutTache();
				setEnabled(false);
				monAjoutTache.setVisible(true);
			}
		});
		btnAjouterUneTache.setBounds(264, 9, 155, 23);
		lydPaneListerTaches.add(btnAjouterUneTache);

		txtMatriculeCollaborateur = new JTextField();
		txtMatriculeCollaborateur.setEditable(false);
		txtMatriculeCollaborateur.setColumns(10);
		txtMatriculeCollaborateur.setBounds(140, 129, 71, 20);
		lydPaneListerTaches.add(txtMatriculeCollaborateur);

		txtReferenceContrat = new JTextField();
		txtReferenceContrat.setEditable(false);
		txtReferenceContrat.setColumns(10);
		txtReferenceContrat.setBounds(140, 154, 71, 20);
		lydPaneListerTaches.add(txtReferenceContrat);

		JComboBox cbxLibelleTache = new JComboBox();
		cbxLibelleTache.setBounds(140, 79, 199, 20);
		lydPaneListerTaches.add(cbxLibelleTache);

		JLabel lblNumero = new JLabel("Num\u00E9ro :");
		lblNumero.setAlignmentX(1.0f);
		lblNumero.setBounds(10, 54, 71, 14);
		lydPaneListerTaches.add(lblNumero);

		JComboBox cbxNumero = new JComboBox();
		cbxNumero.setBounds(140, 51, 199, 20);
		lydPaneListerTaches.add(cbxNumero);

		lydPaneListerCollaborateurs = new JLayeredPane();
		Onglets.addTab("Collaborateurs", null, lydPaneListerCollaborateurs,
				null);
		lydPaneListerCollaborateurs.setLayout(null);

		JLabel lblNomCollab = new JLabel("Nom");
		lblNomCollab.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lblNomCollab.setAlignmentX(1.0f);
		lblNomCollab.setBounds(12, 83, 71, 14);
		lydPaneListerCollaborateurs.add(lblNomCollab);

		JLabel lblCollaborateurs = new JLabel("COLLABORATEUR");
		lblCollaborateurs.setForeground(Color.BLUE);
		lblCollaborateurs.setFont(new Font("Segoe UI", Font.BOLD, 14));
		lblCollaborateurs.setBounds(10, 11, 151, 14);
		lydPaneListerCollaborateurs.add(lblCollaborateurs);

		JLabel lblPrenomCollab = new JLabel("Pr\u00E9nom");
		lblPrenomCollab.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lblPrenomCollab.setBounds(10, 108, 107, 14);
		lydPaneListerCollaborateurs.add(lblPrenomCollab);

		JLabel lblMatricule = new JLabel("Matricule collaborateur :");
		lblMatricule.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lblMatricule.setBounds(12, 58, 135, 14);
		lydPaneListerCollaborateurs.add(lblMatricule);

		txtPrenom = new JTextField();
		txtPrenom.setEditable(false);
		txtPrenom.setColumns(10);
		txtPrenom.setBounds(157, 106, 107, 20);
		lydPaneListerCollaborateurs.add(txtPrenom);

		txtNom = new JTextField();
		txtNom.setEditable(false);
		txtNom.setColumns(10);
		txtNom.setBounds(157, 81, 107, 20);
		lydPaneListerCollaborateurs.add(txtNom);
		
		cbxMatricule.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(declencheur){
				int i = (int) cbxMatricule.getSelectedItem();
				Collaborateur unCollab = dao.getOne(i);
					txtPrenom.setText(unCollab.getPrenom());
					txtNom.setText(unCollab.getNom());
				}
			}
		});
		
		cbxMatricule.setBounds(157, 56, 107, 20);
		lydPaneListerCollaborateurs.add(cbxMatricule);
		/*cbxMatricule.addItem(monCollab.getMatricule());
		cbxMatricule.addItem(unCollab.getMatricule());
		cbxMatricule.addItem(unAutreCollab.getMatricule());*/


		JButton btnAjouterUnCollab = new JButton("Ajouter un collaborateur");
		btnAjouterUnCollab.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnAjouterUnCollab.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				/*
				 * � compl�ter
				 */
				JFrame monAjoutCollab = new ajoutCollab();
				setEnabled(false);
				monAjoutCollab.setVisible(true);
				
			}
		});
		btnAjouterUnCollab.setBounds(262, 9, 157, 23);
		lydPaneListerCollaborateurs.add(btnAjouterUnCollab);
		
		JButton btnRefresh = new JButton("");
		btnRefresh.setIcon(new ImageIcon("C:\\Users\\sramond\\Desktop\\images.jpg"));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dao.getAll();
				chargerListeCollab();
			}
		});
		btnRefresh.setBounds(274, 55, 25, 23);
		lydPaneListerCollaborateurs.add(btnRefresh);
		
		JLayeredPane lydPaneListerContrats = new JLayeredPane();
		lydPaneListerContrats.setLayout(null);
		Onglets.addTab("Contrats", null, lydPaneListerContrats, null);

		JLabel lblContrat = new JLabel("CONTRAT");
		lblContrat.setForeground(Color.BLUE);
		lblContrat.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblContrat.setBounds(10, 13, 71, 14);
		lydPaneListerContrats.add(lblContrat);

		JLabel lblNumRef = new JLabel("Num\u00E9ro de r\u00E9f\u00E9rence : ");
		lblNumRef.setBounds(10, 59, 112, 14);
		lydPaneListerContrats.add(lblNumRef);

		JLabel lblObjetContrat = new JLabel("Objet du contrat : ");
		lblObjetContrat.setBounds(10, 84, 112, 14);
		lydPaneListerContrats.add(lblObjetContrat);

		JLabel lblNomClient = new JLabel("Nom du client : ");
		lblNomClient.setBounds(10, 109, 112, 14);
		lydPaneListerContrats.add(lblNomClient);

		txtClient = new JTextField();
		txtClient.setEditable(false);
		txtClient.setColumns(10);
		txtClient.setBounds(132, 109, 125, 20);
		lydPaneListerContrats.add(txtClient);

		txtObjetContrat = new JTextField();
		txtObjetContrat.setEditable(false);
		txtObjetContrat.setColumns(10);
		txtObjetContrat.setBounds(132, 84, 125, 20);
		lydPaneListerContrats.add(txtObjetContrat);

		JComboBox cbxRef = new JComboBox();
		cbxRef.setBounds(132, 56, 71, 20);
		lydPaneListerContrats.add(cbxRef);
		
		//Chargement liste collab
		chargerListeCollab();

		JButton btnAjouterContrat = new JButton("Ajouter un contrat");
		btnAjouterContrat.setBounds(264, 11, 155, 23);
		lydPaneListerContrats.add(btnAjouterContrat);

		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.inactiveCaption);
		getContentPane().add(panel, BorderLayout.SOUTH);

		JButton btnModifier = new JButton("Modifier");
		panel.add(btnModifier);

		JButton btnSupprimer = new JButton("Supprimer");
		/*
		 * Action bouton supprimer
		 */
		btnSupprimer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switch(Onglets.getSelectedIndex()) {
				//Tache
				case 0:
					
					break;
				//Collab
				case 1: 
					Collaborateur CurrCollab = dao.getOne((Integer)cbxMatricule.getSelectedItem());
					int res = JOptionPane.showConfirmDialog(null, "Confirmer suppression de : " + CurrCollab.getNom()  + " " + CurrCollab.getPrenom(), "Suppression ?", JOptionPane.OK_CANCEL_OPTION);
					if (res == JOptionPane.OK_OPTION) {
						dao.delete(CurrCollab);
						chargerListeCollab();
						JOptionPane.showMessageDialog(null, CurrCollab.getNom()  + " " + CurrCollab.getPrenom() + " supprimé !");
					}
					else
					{
						JOptionPane.showMessageDialog(null,"Suppression annulée !");
					}
					break;
				//Contrat
				case 2:
					
					break;
				
				}
			}
		});
		
		panel.add(btnSupprimer);

		JButton btnQuitter = new JButton("Quitter"); 
		btnQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				quitterAppli();
			}
		});
		panel.add(btnQuitter);
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	
	public void quitterAppli() {
		int res = JOptionPane.showConfirmDialog(null, "Quitter l'application ?", "Quitter ?", JOptionPane.OK_CANCEL_OPTION);
		if (res == JOptionPane.OK_OPTION) {
			System.exit(JFrame.EXIT_ON_CLOSE);
		}
	}
}